/**
 * 
 */
package be.jaisy.osgi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

/**
 * @author Stany Blanvalet
 */
public abstract class AbstractActivator implements BundleActivator {

	protected String BUNDLE_SYMBOLIC_NAME;

	private ServiceTracker logServicetracker;
	
	private static ServiceReference packageAdminServiceReference;
	
	private static PackageAdmin packageAdmin;

	private static Map<Bundle, List<LogService>> logServices = new HashMap<Bundle, List<LogService>>();

	public AbstractActivator() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext context) throws Exception {
		BUNDLE_SYMBOLIC_NAME = context.getBundle().getSymbolicName();
		if(packageAdmin==null){
			packageAdminServiceReference = context.getServiceReference(PackageAdmin.class.getName()); 
			if(packageAdminServiceReference!=null)
				packageAdmin = (PackageAdmin)context.getService(packageAdminServiceReference);
		}
		synchronized (logServices) {
			logServices.put(context.getBundle(), new ArrayList<LogService>());
		}
		logServicetracker = new ServiceTracker(context, LogService.class.getName(), null) {

			public Object addingService(ServiceReference reference) {
				LogService logService = (LogService) context.getService(reference);
				synchronized (logServices) {
					logServices.get(context.getBundle()).add(logService);
				}
				return logService;
			}

			public void modifiedService(ServiceReference reference, Object service) {
				LogService logService = (LogService) context.getService(reference);
				synchronized (logServices) {
					logServices.get(context.getBundle()).add(logService);
				}
			}

			public void removedService(ServiceReference reference, Object service) {
				LogService logService = (LogService) context.getService(reference);
				synchronized (logServices) {
					logServices.get(context.getBundle()).remove(logService);
				}
			}
		};
		logServicetracker.open();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		this.logServicetracker.close();		
		synchronized (logServices) {
			if(packageAdmin!=null)
				context.ungetService(packageAdminServiceReference);
			logServices.remove(context.getBundle());
		}
		BUNDLE_SYMBOLIC_NAME = null;		
	}
	
	public static void logDebug(Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {
			for (LogService logService : logServices.get(bundle)) {
				logService.log(LogService.LOG_DEBUG, "  [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}
	
	public static void logDebug(ServiceReference sr, Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {
			for (LogService logService : logServices.get(bundle)) {
				logService.log(sr, LogService.LOG_DEBUG, "  [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}
	
	public static void logInfo(Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {
			for (LogService logService : logServices.get(bundle)) {
				logService.log(LogService.LOG_INFO, "  [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}
	
	public static void logInfo(ServiceReference sr, Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {
			for (LogService logService : logServices.get(bundle)) {
				logService.log(sr, LogService.LOG_INFO, "  [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}

	public static void logWarning(Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {

			for (LogService logService : logServices.get(bundle)) {
				logService.log(LogService.LOG_WARNING, " [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}

	public static void logWarning(ServiceReference sr, Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {

			for (LogService logService : logServices.get(bundle)) {
				logService.log(sr, LogService.LOG_WARNING, " [" + requestor.getSimpleName() + "] " + message);
			}
		}
	}
	
	public static void logWarning(Class requestor, String message, Throwable t) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {

			for (LogService logService : logServices.get(bundle)) {
				logService.log(LogService.LOG_WARNING, " [" + requestor.getSimpleName() + "] " + message, t);
			}
		}
	}
	
	public static void logWarning(ServiceReference sr, Class requestor, String message, Throwable t) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		synchronized (logServices) {

			for (LogService logService : logServices.get(bundle)) {
				logService.log(sr, LogService.LOG_WARNING, " [" + requestor.getSimpleName() + "] " + message, t);
			}
		}
	}

	public static void logError(Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		for (LogService logService : logServices.get(bundle)) {
			logService.log(LogService.LOG_ERROR, " [" + requestor.getSimpleName() + "] " + message);
		}
	}
	
	public static void logError(ServiceReference sr, Class requestor, String message) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		for (LogService logService : logServices.get(bundle)) {
			logService.log(sr, LogService.LOG_ERROR, " [" + requestor.getSimpleName() + "] " + message);
		}
	}
	
	public static void logError(Class requestor, String message, Throwable t) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		for (LogService logService : logServices.get(bundle)) {
			logService.log(LogService.LOG_ERROR, " [" + requestor.getSimpleName() + "] " + message, t);
		}
	}
	
	public static void logError(ServiceReference sr, Class requestor, String message, Throwable t) {
		Bundle bundle = packageAdmin.getBundle(requestor);
		for (LogService logService : logServices.get(bundle)) {
			logService.log(sr, LogService.LOG_ERROR, "["+bundle.getSymbolicName()+"][" + requestor.getSimpleName() + "] " + message, t);
		}
	}
}
